package zqm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import zqm.controller.dto.UserDTO;
import zqm.controller.dto.UserPasswordDTO;
import zqm.entity.User;
import zqm.exception.ServiceException;
import zqm.mapper.UserMapper;
import zqm.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private static final Log LOG = Log.get();

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDTO login(UserDTO userDTO) {
        userDTO.setPassword(SecureUtil.md5(userDTO.getPassword()));
        User one = getUserInfo(userDTO);
        if (one != null) {
            BeanUtil.copyProperties(one, userDTO, true);

            // 硬编码menus
            List<Object> menus = new ArrayList<>();
            // 首页
            java.util.Map<String, Object> home = new java.util.HashMap<>();
            home.put("id", 1);
            home.put("name", "首页");
            home.put("path", "/home");
            home.put("pagePath", "Home");
            home.put("icon", "el-icon-house");
            home.put("children", new ArrayList<>());
            menus.add(home);
            // 个人信息
            java.util.Map<String, Object> person = new java.util.HashMap<>();
            person.put("id", 2);
            person.put("name", "个人信息");
            person.put("path", "/person");
            person.put("pagePath", "Person");
            person.put("icon", "el-icon-user");
            person.put("children", new ArrayList<>());
            menus.add(person);
            // 修改密码
            java.util.Map<String, Object> password = new java.util.HashMap<>();
            password.put("id", 3);
            password.put("name", "修改密码");
            password.put("path", "/password");
            password.put("pagePath", "Password");
            password.put("icon", "el-icon-lock");
            password.put("children", new ArrayList<>());
            menus.add(password);
            // ...如需添加其它菜单项，仿照上面添加
            userDTO.setMenus(menus);
            return userDTO;
        } else {
            throw new ServiceException("600", "用户名或密码错误");
        }
    }

    @Override
    public User register(UserDTO userDTO) {
        // 用户密码 md5加密
        userDTO.setPassword(SecureUtil.md5(userDTO.getPassword()));
        User one = getUserInfo(userDTO);
        if (one == null) {
            one = new User();
            BeanUtil.copyProperties(userDTO, one, true);
            // 默认一个普通用户的角色
            one.setRole("ROLE_USER".toString());
            if (one.getNickname() == null) {
                one.setNickname(one.getUsername());
            }
            save(one);  // 把 copy完之后的用户对象存储到数据库
        } else {
            throw new ServiceException("600", "用户已存在");
        }
        return one;
    }

    @Override
    public void updatePassword(UserPasswordDTO userPasswordDTO) {
        int update = userMapper.updatePassword(userPasswordDTO);
        if (update < 1) {
            throw new ServiceException("600", "密码错误");
        }
    }



    private User getUserInfo(UserDTO userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userDTO.getUsername());
        queryWrapper.eq("password", userDTO.getPassword());
        User one;
        try {
            one = getOne(queryWrapper); // 从数据库查询用户信息
        } catch (Exception e) {
            LOG.error(e);
            throw new ServiceException("500", "系统错误");
        }
        return one;
    }



}
